package psn.zjk.springboot.service;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Configuration;

/**
 * @Description
 * @User zero
 * @Datetime 2021/6/6 18:44
 */
@Aspect
@Configuration
public class AopTest2 {


    //切面方法貌似，别写实现与参数，不然@Before引用貌似有问题
    @Pointcut("execution(* AopService.*(..))")
    public void x() {}

    @Pointcut("execution(* AopService.*(..))")
    public void x2() {}

    @Pointcut("@within(org.springframework.stereotype.Service)")
    public void x1() {
    }

    @Pointcut("x1()||x2()")
    public void x3() {
    }

   /** 每个@Before都是一个 advisor，没有advisor，切面就白切了
    * 源码：
    * {@link org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#createProxy} */
    @Before("x()")
    public void out(JoinPoint joinPoint) {
        System.out.println(joinPoint.getTarget());
    }

    /**
     * {@link org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor#invoke}
     *  advisors的执行是递归的  mi.proceed()会触发下一个advisor。所以，advisor太多会影响原来程序的性能。
     * */
    @Before("x1()")
    public void out1(JoinPoint joinPoint) {
        System.out.println("2222222222222"+joinPoint.getTarget());
    }

    public static void main(String[] args) {
        SpringApplication.run(psn.zjk.springboot.service.AopTest2.class);
    }


}
